$(document).ready(
		function() {
			getNowUserInfo();
			loadFollow();

			_getPosts(0, 1);

			$(document).on(
					'click',
					".passage",
					function() {
						console.log($(this).find('a')[0]);
						var postId = $(this).find('a').attr('value');
						window.location.href = 'inner_post.html?postid='
								+ postId + '&pageNumber=1';
					});

			$(document).on('click', '.ff', function() {
				var userid = $(this).attr("value");
				console.log(userid);
				window.location.href = "other_homepage.html?userid=" + userid;
			});

			$(document).on('click', '.delete', function() {
				var postid = $(this).parent().attr('value');
				deletePost(postid);
				getPostsOfNowUser(1)
			});
		});

function getNowUserInfo() {
	user = getNowUser();
	$("#userName").text(user['userName']);
	$("#userDesc").text(user['description']);
	$("#headScul").attr("src", user['headImg']);
}

function loadFollow() {
	$("#headDiv").empty();
	concers = getConcernUsers(0);
	console.log(concers);
	if (concers != undefined) {
		for (i = 0; i < concers.length; i++) {
			var concer = concers[i];
			console.log(concer);
			var appendDiv = '<div class="ff" value='
					+ concer['userid']
					+ ' style="text-align: center; width: 112px; height: 112px; margin-top: 5px; float: left">'
					+ '<img src="'
					+ concer['headImg']
					+ '" style="width: 80px; height: 80px; border-radius: 50%"><br>'
					+ '<div style="font: 15px 微软雅黑; margin: auto">'
					+ concer['userName'] + '</div>' + '</div>';
			$("#headDiv").append(appendDiv).trigger("create");
		}
	}

}

function loadFans() {
	$("#headDiv").empty();
	concers = getFollowers(0);
	if (concers != undefined) {
		for (i = 0; i < concers.length; i++) {
			var concer = concers[i];
			console.log(concer);
			var appendDiv = '<div class="ff" value='
					+ concer['userid']
					+ ' style="text-align: center; width: 112px; height: 112px; margin-top: 5px; float: left">'
					+ '<img src="'
					+ concer['headImg']
					+ '" style="width: 80px; height: 80px; border-radius: 50%"><br>'
					+ '<div style="font: 15px 微软雅黑; margin: auto">'
					+ concer['userName'] + '</div>' + '</div>';
			$("#headDiv").append(appendDiv).trigger("create");
		}
	}
}

function _getPosts(type, pageNumber) {
	$("#posts").empty();
	$("#page").empty();
	if (type == 0) {
		var posts = getPostsOfUser(0, pageNumber);
	} else if (type == 1) {
		var posts = getUserAgreePosts(0, pageNumber);
	} else if (type == 2) {
		var posts = getUserCollectPosts(0, pageNumber);
	}
	var count = getPostsTotalNumber(type, 0);
	totalPage = Math.ceil(count / 3);
	if (totalPage == 0) {
		$("#posts").append(
				'<div><h4 style="text-align:center">该版块暂无帖子</h4></div>');
		return;
	}
	for (i = 0; i < posts.length; i++) {
		post = posts[i];
		container = '<div class="container"> ' + ' <div class="user-image"> '
				+ ' <a href="#"><img src= '
				+ post['user']['headImg']
				+ ' id="userImage" style="width:30px,height:20px;" '
				+ ' width="30" height="30"></a> </div> '
				+ ' <div class="user-name"> '
				+ ' <a id="userName" href="other_homepage.html?userid='
				+ post['user']['userid']
				+ '">'
				+ post['user']['userName']
				+ '</a> </div> '
				+ ' <div class="passage"> '
				+ ' <a href="#" value='
				+ post['post']['postid']
				+ ' id="passageName">'
				+ post['post']['title']
				+ '</a> '
				+ ' <p id="date">'
				+ post['post']['time']
				+ '</p> '
				+ '  <p id="passageMain"> '
				+ post['post']['txtContent']
				+ ' <br> '
				+ ' </div> '
				+ ' <div class="btnCol" value='
				+ post['post']['postid']
				+ '> <button  class="delete">'
				+ '删除'
				+ '</button> ' + '</div> '
		' </div> ';

		$("#posts").append(container);

	}
	if (totalPage > 1) {
		createPage(totalPage, pageNumber, type);
	}
}

function createPage(count, start, type) {

	if (count < 3) {
		display = count;
	} else {
		display = 3;
	}
	$("#page").paginate({
		count : count,
		start : start,
		display : display,
		border : true,
		border_color : '#BEF8B8',
		text_color : '#68BA64',
		background_color : '#E3F2E1',
		border_hover_color : '#68BA64',
		text_hover_color : 'black',
		background_hover_color : '#CAE6C6',
		rotate : false,
		images : false,
		mouse : 'press',
		onChange : function(page_index) {
			_getPosts(type, page_index);
		}
	});
}
